{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "#### ICS 104 - Introduction to Programming in Python and C\n",
    "# Overview of C Language\n",
    "## Reading Assignment"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- Chapter 2: Sections 1-6.\n",
    "- Chapter 3: Section 1.\n",
    "- Chapter 4: Sections 1-4 and 7.\n",
    "- Chapter 5: Sections 1-8."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Learning Outcomes  \n",
    "After going through these chapters, it is expected that we\n",
    "- become familiar with the general form of a <font color='blue'>**C program**</font> and the basic elements in a program. \n",
    "- understand the use of **<font color='blue'>data types</font>** and the differences between the data types `int`, `double` and `char`.\n",
    "- learn how **<font color='blue'>C evaluates arithmetic expressions</font>** and how to write them in C.\n",
    "- develop C code using **<font color='blue'>Control structures, conditions, relational and logic operators</font>**.\n",
    "- develop C code using The **<font color='blue'>while, for and do-while</font>** loops."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Why Learn C?\n",
    "- Many companies and software projects do their programming in C.\n",
    "- C Produces optimized programs that run fast.\n",
    "- Low-Level access to computer memory via pointers.\n",
    "- Can be complied to run on a variety of computers. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# C Language Elements - Example\n",
    "- We will use the online C compiler in https://www.onlinegdb.com/online_c_compiler"
   ]
  },
  {
   "attachments": {
    "C-book-ch02fig1.PNG": {
     "image/png": "[encoded data, 108300 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {
    "cell_style": "center",
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "![C-book-ch02fig1.PNG](attachment:C-book-ch02fig1.PNG)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_style": "center",
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "```C\n",
    "\n",
    "/*\n",
    "\n",
    " * Converts distances from miles to kilometers.\n",
    "\n",
    " */\n",
    "\n",
    "#include <stdio.h>           /* printf, scanf definitions */\n",
    "\n",
    "#define KMS_PER_MILE 1.609   /* conversion constant */\n",
    "\n",
    "int\n",
    "\n",
    "main(void)\n",
    "\n",
    "{\n",
    "\n",
    "    double miles,    /* distance in miles */\n",
    "    \n",
    "           kms;      /* equivalent distance in kilometers */\n",
    " \n",
    "\n",
    "    /* Get the distance in miles. */\n",
    " \n",
    "    printf(\"Enter the distance in miles> \");\n",
    " \n",
    "    scanf(\"%lf\", &miles);\n",
    " \n",
    " \n",
    "    /* Convert the distance to kilometers. */\n",
    " \n",
    "    kms = KMS_PER_MILE * miles;\n",
    " \n",
    " \n",
    "    /* Display the distance in kilometers. */\n",
    " \n",
    "    printf(\"That equals %f kilometers.\\n\", kms);\n",
    " \n",
    "    return (0);\n",
    "\n",
    "}\n",
    "\n",
    "\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_style": "split",
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Preprocessor Directives\n",
    "- The C program has two parts:\n",
    "    - preprocessor directives; and \n",
    "    - the main function"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_style": "split",
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- The preprocessor directives are commands that give instructions to\n",
    "the C preprocessor, whose job it is to modify the text of a C program before it\n",
    "is compiled. \n",
    "- A preprocessor directive begins with a number symbol (#) as its first nonblank character. \n",
    "- The two most common directives appearing in the previous figure are: \n",
    "    - `#include` \n",
    "    - `#define.`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_style": "center",
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "- The C language explicitly defines only a small number of operations: Many actions that are necessary in a computer program are not defined directly by C.\n",
    "- Instead, every C implementation contains collections of useful functions and symbols called libraries. \n",
    "- The ANSI (American National Standards Institute) standard for C requires that certain standard libraries be provided in every ANSI C implementation. \n",
    "- A C system may expand the number of operations available by supplying additional libraries; an individual programmer can also create libraries of functions. \n",
    "- Each library has a standard header file whose name ends with the symbols `.h`."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "- The `#include` directive gives a program access to a library. \n",
    "- This directive causes the preprocessor to insert definitions from a standard header file into a program before compilation. The directive\n",
    "\n",
    "    - `#include <stdio.h> /*printf, scanf definitions*/`\n",
    "    \n",
    "- notifies the preprocessor that some names used in the program (such as `scanf` and `printf`) are found in the standard header file `<stdio.h>`."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- The other preprocessor directive\n",
    "    - `#define KMS_PER_MILE 1.609 /*conversion constant*/`\n",
    "- associates the constant macro `KMS_PER_MILE` with the meaning 1.609. This directive instructs the preprocessor to replace each occurrence of `KMS_PER_MILE` in the text of the C program by 1.609 before compilation begins."
   ]
  },
  {
   "attachments": {
    "C-book-ch02fig2.PNG": {
     "image/png": "[encoded data, 113756 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Syntax Displays for Preprocessor Directives\n",
    "![C-book-ch02fig2.PNG](attachment:C-book-ch02fig2.PNG)"
   ]
  },
  {
   "attachments": {
    "C-book-ch02fig2-1.PNG": {
     "image/png": "[encoded data, 72476 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "![C-book-ch02fig2-1.PNG](attachment:C-book-ch02fig2-1.PNG)"
   ]
  },
  {
   "attachments": {
    "C-book-ch02fig3.PNG": {
     "image/png": "[encoded data, 14712 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Function main\n",
    "![C-book-ch02fig3.PNG](attachment:C-book-ch02fig3.PNG)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- **int main(void)** marks the beginning of the main function where program execution begins.\n",
    "- **Every C program has a main function.**\n",
    "- **Braces {** and **}** mark the beginning and end of the body of function main.\n",
    "- A function body has two parts:\n",
    "    - Declarations - tell the compiler what memory cells are needed in the function\n",
    "    - Executable statements - (derived from the algorithm) are translated into machine language and later executed by the computer"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Reserved Words\n",
    "- A word that has special meaning to C and can not be used for other purposes.\n",
    "- These are words that C reserves for its own uses\n",
    "- Built-in Types: **int, double, char, void, etc.**\n",
    "- Control flow: **if, else, for, while, return, etc.**\n",
    "- Always lower case."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Standard Identifiers\n",
    "- Standard identifier is a word having special meaning but one that a programmer may redefine\n",
    "    - But redefinition is not recommended. \n",
    "- Examples: \n",
    "    - `printf`, `scanf`\n",
    "- For example, if you define your own function `printf`, then you cannot use the C library function `printf`."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# User-Defined Identifiers\n",
    "- We choose our own identifiers to\n",
    "    - Name memory cells that will hold data and program results\n",
    "    - Name functions that we define\n",
    "- Rules for Naming Identifiers:\n",
    "    - An identifier consists only of letters, digits, and underscores\n",
    "    - An identifier cannot begin with a digit\n",
    "    - A C reserved word cannot be used as an identifier\n",
    "    - A standard C identifier should not be redefined\n",
    "- Examples of Valid identifiers:\n",
    "    - **letter1, inches, KM_PER_MILE**\n",
    "- Examples of Invalid identifiers: \n",
    "    - **1letter, Happy$trout, return**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Hello World! Example"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "```C\n",
    "\n",
    "#include <stdio.h>  \n",
    "\n",
    "int main() {  \n",
    "\n",
    "    // printf() displays the string inside quotation  \n",
    "\n",
    "    printf(\"Hello, World!\");  \n",
    "\n",
    "    return 0;\n",
    "\n",
    "}\n",
    "\n",
    "\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Key Points for Python Programmers\n",
    "- The first thing a Python programmer notices is significant overhead to simply print a string. \n",
    "- The **#include** works like import in Python (not really, but the analogy works for now) so in this case the input/output stream library is brought in.  \n",
    "- The `int main()` begins the declaration of the main function, which in **C** indicates the first function to be executed, i.e. \"begin here\". In Python, `main` was the start of the program **by convention only**. \n",
    "- The curly braces { and } delimit the beginning and end of a block of code (compound statement), in this case the beginning and end of the function `main`.  Python identifies blocks through indentation."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Variable Declarations"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_style": "split",
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "\n",
    "- **Variables:** The memory cells used for storing a program’s input data and its computational results \n",
    "    - The Value of a Variable can change at runtime\n",
    "- **Variable declarations:** Statements that communicate to the compiler the names of variables in the program and the type of data they can store.\n",
    "    - Examples:\n",
    "    \n",
    "```C\n",
    "\n",
    "    double miles, kms;\n",
    "\n",
    "    int count, large;\n",
    "\n",
    "    char answer;\n",
    "\n",
    "\n",
    "```\n",
    "\n",
    "- C requires that you declare every variable in the program."
   ]
  },
  {
   "attachments": {
    "C-book-ch02fig3-1.PNG": {
     "image/png": "[encoded data, 93308 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {
    "cell_style": "split",
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "![C-book-ch02fig3-1.PNG](attachment:C-book-ch02fig3-1.PNG)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Key Points for Python Programmers\n",
    "- In **C**, all variables must be declared before use. \n",
    "- Python figures out types based on the objects you assign names to, but **C** wants to know up front.\n",
    "- Python allows names to refer to different types of objects while the program is running; in **C** the type remains fixed."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Data Types\n",
    "- Data Types: a set of values and a set of operations that can be performed on those values\n",
    "    - int: Stores signed integer values: whole numbers\n",
    "        -Examples: 65, -12345\n",
    "    - double: Stores real numbers that use a decimal point\n",
    "        - Examples: 3.14159 or 1.23e5 (which equals 123000.0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "## Key Points for Python Programmers\n",
    "- Some C types are similar:\n",
    "    - int, float, string\n",
    "- Data type char is different:"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Data Type char\n",
    "- char represents an individual character value\n",
    "    - a letter, a digit or a special symbol.\n",
    "- Each value of type `char` is enclosed in apostrophes (single quotes): \n",
    "    - For example, `'A','z', ':'`"
   ]
  },
  {
   "attachments": {
    "C-book-ch02fig3-2.PNG": {
     "image/png": "[encoded data, 24888 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- ASCII code: a particular code that specifies the integer representing each char value. \n",
    "![C-book-ch02fig3-2.PNG](attachment:C-book-ch02fig3-2.PNG)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Input/Output Operations and Functions \n",
    "- **Input operation**: data transfer from the outside world into computer memory\n",
    "- **Output operation**: program results can be displayed to the program user\n",
    "- **Input/Output functions**: special program units that do all input/output operations\n",
    "    - printf : output function\n",
    "    - scanf : input function\n",
    "- **Function call**: used to call or activate a function\n",
    "    - Asking another piece of code to do some work for you"
   ]
  },
  {
   "attachments": {
    "C-book-ch02fig4.PNG": {
     "image/png": "[encoded data, 17092 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "![C-book-ch02fig4.PNG](attachment:C-book-ch02fig4.PNG)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- Output: That equals 16.090000 Kilometers."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Placeholders\n",
    "- Placeholders always begin with the symbol %\n",
    "    - % marks the place in a format string where a value will be printed out or will be read\n",
    "- Format strings can have multiple placeholders, if you are printing multiple values"
   ]
  },
  {
   "attachments": {
    "C-book-ch02fig5.PNG": {
     "image/png": "[encoded data, 32336 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "![C-book-ch02fig5.PNG](attachment:C-book-ch02fig5.PNG)"
   ]
  },
  {
   "attachments": {
    "C-book-ch02fig6.PNG": {
     "image/png": "[encoded data, 27316 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# The scanf Function\n",
    "![C-book-ch02fig6.PNG](attachment:C-book-ch02fig6.PNG)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- The **&** is the address (reference) operator. It tells scanf the address of variable miles in memory.\n",
    "    - The reason is that parameters in function calls in C are all called **by value**.\n",
    "        - Exactly like Python.\n",
    "- When user inputs a value, it is stored in miles.\n",
    "- The placeholder **%lf** tells **scanf** the type of data to store into variable miles."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Arithmetic Expressions\n",
    "- To solve most programming problems, you need to write arithmetic expressions that compute data of type **int** and **double** (and sometimes **char**)\n",
    "- Arithmetic expressions contain variables, constants, function calls, arithmetic operators, as well as sub-expressions written within parentheses."
   ]
  },
  {
   "attachments": {
    "C-book-ch02fig8.PNG": {
     "image/png": "[encoded data, 46924 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "![C-book-ch02fig8.PNG](attachment:C-book-ch02fig8.PNG)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Data Type of an Expression\n",
    "- What is the type of expression **x+y** when **x** and **y** are both of type **int**? \n",
    "    - (answer: type of **x+y** is **int**)\n",
    "- The data type of an expression depends on the type(s) of its operands\n",
    "    - If both are of type **int**, then the expression is of type **int**.\n",
    "    - If either one or both operands are of type **double**, then the expression is of type **double**.\n",
    "- An expression that has mixed operands of type **int** and **double** is a mixed-type expression."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Mixed-Type Assignment Statement\n",
    "- The expression being evaluated and the variable to which it is assigned have **different data types**\n",
    "- The expression is first evaluated; then the result is assigned to the variable to the left side of **=** operator \n",
    "    - Example: what is the value of  y = 5/2  when y is of type **double**? (answer: **5/2** is **2; y = 2.0**)\n",
    "- **Warning:** assignment of a type **double** expression to a type **int** variable causes the fractional part of the expression to be lost.\n",
    "    - Example: what is the type of the assignment **y = 5.0 / 2.0** when **y** is of type **int**? (answer: **5.0/2.0 is 2.5; y = 2** )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Type Conversions Through CASTS\n",
    "- C allows the programmer to convert the type of an expression by placing the desired type in parentheses before the expression.\n",
    "- This operation is called a **type cast**. \n",
    "    - **(double)5 / (double)2** is the **double** value **2.5**\n",
    "    - **(int)(9 * 0.5)** is the **int** value **4**\n",
    "- When casting from **double** to **int**, the decimal fraction is truncated (NOT rounded)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "```C \n",
    "\n",
    "/* Computes a test average  */\n",
    "\n",
    "#include <stdio.h>\n",
    "\n",
    "\n",
    "\n",
    "int main(void)\n",
    "\n",
    "{\n",
    "  \n",
    "    int     total;     /* total score */\n",
    "  \n",
    "    int     students;  /* number of students */\n",
    "  \n",
    "    double  average;   /* average score */\n",
    "\n",
    "  \n",
    "    printf(\"Enter total students score> \");\n",
    "  \n",
    "    scanf(\"%d\", &total);\n",
    "  \n",
    "    printf(\"Enter number of students> \");\n",
    "  \n",
    "    scanf(\"%d\", &students);\n",
    "  \n",
    "    average = (double) total / (double) students;\n",
    "  \n",
    "    printf(\"Average score is %.2f\\n\", average);\n",
    "  \n",
    "    return 0;\n",
    "}\n",
    "\n",
    "\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Expression with Multiple Operators\n",
    "- Operators are of two types: **unary** and **binary**\n",
    "- **Unary operators** take only one operand\n",
    "    - Unary minus (**-**) and Unary plus (**+**) operators\n",
    "- **Binary operators** take two operands\n",
    "    - Examples: addition (**+**), subtraction (**–**), multiplication (*****), division (**/**) and integer remainder (**%**) operators\n",
    "- A single expression could have multiple operators\n",
    "    - Example: **-a + b*c – d/2**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Rules for Evaluating Expressions\n",
    "- **Parentheses rule** - All expressions in parentheses must be evaluated separately.\n",
    "    - Nested parenthesized expressions must be evaluated from the inside out, with the innermost expression evaluated first.\n",
    "- **Operator precedence rule** – Multiple operators in the same expression are evaluated in the following order:\n",
    "    - First: \t**unary +, –** \n",
    "    - Second: \t*****, **/**, **%**\n",
    "    - Third: \t**binary +, –**\n",
    "- **Associativity rule**\n",
    "    - **Unary operators** in the same subexpression and at the same precedence level are **evaluated right to left**\n",
    "    - **Binary operators** in the same subexpression and at the same precedence level are **evaluated left to right**"
   ]
  },
  {
   "attachments": {
    "C-book-ch02fig8-1.PNG": {
     "image/png": "[encoded data, 19660 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {
    "cell_style": "split",
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Step-By-Step Expression Evaluation\n",
    "![C-book-ch02fig8-1.PNG](attachment:C-book-ch02fig8-1.PNG)"
   ]
  },
  {
   "attachments": {
    "C-book-ch02fig8-2.PNG": {
     "image/png": "[encoded data, 16344 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {
    "cell_style": "split",
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "![C-book-ch02fig8-2.PNG](attachment:C-book-ch02fig8-2.PNG)"
   ]
  },
  {
   "attachments": {
    "C-book-ch02fig8-3.PNG": {
     "image/png": "[encoded data, 43160 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "![C-book-ch02fig8-3.PNG](attachment:C-book-ch02fig8-3.PNG)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "## Key Points for Python Programmers\n",
    "- Arithmetic expression in C are similar to Python:\n",
    "    - +,-,*,/,%\n",
    "    - +=, -=,*=,/="
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Selection Structure\n",
    "- In C, selection structures are similar to those in Python, but \n",
    "    - parentheses are required around the expression,\n",
    "    - semi-colon is required, if the body of the `if` is a single statement, and \n",
    "    - indentation is not required, but highly recommended."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_style": "split",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "source": [
    "```C  \n",
    "  \n",
    "if ( <condition> )  \n",
    "\n",
    "    statement;  \n",
    "  \n",
    "  \n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_style": "split",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "source": [
    "```C  \n",
    "  \n",
    "if ( <condition> )  {\n",
    "\n",
    "    statement1;  \n",
    "    \n",
    "    statement2;  \n",
    "    \n",
    "    ...  \n",
    "    \n",
    "}\n",
    "  \n",
    "  \n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_style": "center",
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "- For example,\n",
    "\n",
    "```C\n",
    "\n",
    "    if (x > 4)\n",
    "        \n",
    "        y = 7;\n",
    "\n",
    "\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_style": "split",
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "## Key Points for Python Programmers\n",
    "- Like Python, C allows an \"else\" clause:\n",
    "\n",
    "```C\n",
    "    \n",
    "    if (x <= 5)\n",
    "        \n",
    "        y = 8;\n",
    "\n",
    "    else\n",
    "        \n",
    "        z = 10;\n",
    "\n",
    "\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_style": "split",
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "## Key Points for Python Programmers\n",
    "- C does not have the \"elif\" keyword. Instead, you must use \"else if\".\n",
    "\n",
    "```C\n",
    "    \n",
    "    if (y == 7)\n",
    "        \n",
    "        x = 2;\n",
    "\n",
    "    else if (x != 2)\n",
    "        \n",
    "            y = 5;\n",
    "\n",
    "     else\n",
    "\n",
    "            z = 10;\n",
    "\n",
    "\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Key Points for Python Programmers\n",
    "- Python uses indentation to indicate a block of statement(s).\n",
    "- C uses curly braces, { and } to delimit blocks of statements.\n",
    "- Indentation is highly recommended. \n",
    "- For example:\n",
    "\n",
    "```C\n",
    "\n",
    "    if ((x <= 5) && (y != 7)) {\n",
    "\n",
    "        z = 12;\n",
    "\n",
    "        w = 13;\n",
    "\n",
    "    }\n",
    "\n",
    "\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Key Points for Python Programmers"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_style": "split",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "source": [
    "- **Relational Operators**\n",
    "\n",
    "|C Operator| Relational Operation |\n",
    "| :---: | :--- |\n",
    "|`<=` | Less than or equal |\n",
    "|`<` | Less than |\n",
    "|`>=` | Greater than or equal |\n",
    "|`>` | Greater than |\n",
    "|`==` | Equals |\n",
    "|`!=` | Not Equal to |"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_style": "split",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "source": [
    "-------------------------------\n",
    "\n",
    "- **Logical Operators**\n",
    "\n",
    "|C Operator| Logical Operation |\n",
    "| :---: | :--- |\n",
    "| `!` | Logical Not |\n",
    "| `&&` | Logical And |\n",
    "| `\\|\\|` | Logical Or |\n"
   ]
  },
  {
   "attachments": {
    "C-book-ch02fig12-5.PNG": {
     "image/png": "[encoded data, 58580 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {
    "cell_style": "split",
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "![C-book-ch02fig12-5.PNG](attachment:C-book-ch02fig12-5.PNG)"
   ]
  },
  {
   "attachments": {
    "C-book-ch02fig12-6.PNG": {
     "image/png": "[encoded data, 64316 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {
    "cell_style": "split",
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "![C-book-ch02fig12-6.PNG](attachment:C-book-ch02fig12-6.PNG)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Note: In the C Language,\n",
    "- There is no **Boolean** type. Results of logical expressions are either `zero` (`false`) or `one` (`true`). \n",
    "- Any nonzero value is considered to be `true`. For example:"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "```C\n",
    "\n",
    "/* true vs. false */\n",
    "\n",
    "#include <stdio.h>\n",
    "\n",
    "int main(void)\n",
    "\n",
    "{\n",
    "\n",
    "  if (1+2)\n",
    "  \n",
    "     printf(\"1 + 2 is true. The value of the condition 1 + 2 == 3 is %d\", 1+2 == 3);\n",
    "\n",
    "  if (1-1)\n",
    "  \n",
    "     printf(\"I should not see this printf statement, since the value is 0\");\n",
    "\n",
    "  return 0;\n",
    "\n",
    "}\n",
    "\n",
    "\n",
    "```"
   ]
  },
  {
   "attachments": {
    "C-book-ch02fig12-7.PNG": {
     "image/png": "[encoded data, 47540 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {
    "cell_style": "split",
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "![C-book-ch02fig12-7.PNG](attachment:C-book-ch02fig12-7.PNG)"
   ]
  },
  {
   "attachments": {
    "C-book-ch02fig12-8.PNG": {
     "image/png": "[encoded data, 136004 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {
    "cell_style": "split",
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "![C-book-ch02fig12-8.PNG](attachment:C-book-ch02fig12-8.PNG)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "#  Student Activity\n",
    "## Do You Remember This Example?"
   ]
  },
  {
   "attachments": {
    "fig11.png": {
     "image/png": "[encoded data, 66948 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {
    "cell_style": "center",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "source": [
    "![fig11.png](attachment:fig11.png)\n",
    "- Develop its implementation in C."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_style": "center",
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "```C\n",
    "\n",
    "#include <stdio.h>\n",
    "\n",
    "#define RATE1 0.10 \n",
    "\n",
    "#define RATE2 0.25 \n",
    "\n",
    "#define RATE1_SINGLE_LIMIT 32000.0  \n",
    "\n",
    "#define RATE1_MARRIED_LIMIT 64000.0  \n",
    "\n",
    "int main() {\n",
    "\n",
    "    double income;\n",
    " \n",
    "    char maritalStatus;\n",
    " \n",
    " \n",
    "    double tax1, tax2;\n",
    " \n",
    "    double totalTax;\n",
    " \n",
    " \n",
    "    /* read income and marital status */\n",
    " \n",
    "    printf(\"Please enter your income: \");\n",
    " \n",
    "    scanf(\"%lf\", &income);\n",
    " \n",
    "    printf(\"Please enter s for single, m for married: \");\n",
    " \n",
    "    scanf(\" %c\", &maritalStatus); \n",
    " \n",
    "    /* The space before %c is to consume any whitespace before reading the character */ \n",
    " \n",
    " \n",
    "    /* Compute taxes due */\n",
    " \n",
    "    tax1 = 0;\n",
    " \n",
    "    tax2 = 0;\n",
    " \n",
    "\n",
    "    /* Enter the if statement[s] here */\n",
    "    \n",
    " \n",
    "    totalTax = tax1 + tax2;\n",
    " \n",
    " \n",
    "    printf(\"The tax is $%.2f\\n\",totalTax);\n",
    " \n",
    " return 0;\n",
    " \n",
    " }\n",
    " \n",
    " \n",
    " ```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Loops"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "## While loop\n",
    "- The C `while` loop will be familiar to you, and shares the same syntax rules with the C `if` statement:\n",
    "    - Parentheses are required for the expression.\n",
    "    - Curly braces delimit a block (required for compound statements)\n",
    "- For example,\n",
    "\n",
    "```C\n",
    "\n",
    "    while (x < 5) {\n",
    "        \n",
    "        y = z + x;\n",
    "        \n",
    "        x = x + 1;\n",
    "        \n",
    "    }\n",
    "\n",
    "\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "### Example"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "source": [
    "```C\n",
    "\n",
    "#include <stdio.h>\n",
    "\n",
    "\n",
    "\n",
    "int\n",
    "\n",
    "main(void)\n",
    "\n",
    "{\n",
    "\n",
    "    double total_pay; /* company payroll */\n",
    "    \n",
    "    int count_emp; /* current employee */\n",
    "    \n",
    "    int number_emp; /* number of employees */\n",
    "    \n",
    "    double hours; /* hours worked */\n",
    "    \n",
    "    double rate; /* hourly rate */\n",
    "    \n",
    "    double pay; /* pay for this period */\n",
    "\n",
    "    \n",
    "    \n",
    "    /* Get number of employees. */\n",
    "    \n",
    "    printf(\"Enter number of employees> \");\n",
    "    \n",
    "    scanf(\"%d\", &number_emp);\n",
    "\n",
    "    \n",
    "    \n",
    "    /* Compute each employee's pay and add it to the payroll. */\n",
    "    \n",
    "    total_pay = 0.0;\n",
    "    \n",
    "    count_emp = 0;\n",
    "    \n",
    "    while (count_emp < number_emp) {\n",
    "    \n",
    "        printf(\"Hours> \");\n",
    "        \n",
    "        scanf(\"%lf\", &hours);\n",
    "        \n",
    "        printf(\"Rate > $\");\n",
    "        \n",
    "        scanf(\"%lf\", &rate);\n",
    "        \n",
    "        pay = hours * rate;\n",
    "        \n",
    "        printf(\"Pay is $%6.2f\\n\\n\", pay);\n",
    "        \n",
    "        total_pay = total_pay + pay; /* Add next pay. */\n",
    "\n",
    "        count_emp = count_emp + 1;\n",
    "\n",
    "    }\n",
    "               \n",
    "\n",
    "    printf(\"All employees processed\\n\");\n",
    "\n",
    "    printf(\"Total payroll is $%8.2f\\n\", total_pay);\n",
    "\n",
    "    return (0);\n",
    "\n",
    "}\n",
    "\n",
    "\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## For Loop\n",
    "- The \"for\" loop in C is **quite different** from Python. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "cell_style": "center",
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "for x in range(5):\n",
    "    print(\"Inside the loop, x =\",x)\n",
    "print(\"Outside the loop, x =\",x) "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_style": "center",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "source": [
    "```C  \n",
    "\n",
    "#include <stdio.h>\n",
    "\n",
    "int main() {\n",
    "    \n",
    "    int x;\n",
    "    \n",
    "    for (x=0; x <= 4; x++)\n",
    "    \n",
    "        printf(\"Inside the for loop, x = %d\\n\", x);\n",
    "    \n",
    "    printf(\"Outside the for loop, x = %d\\n\", x);\n",
    "    \n",
    "    return 0;\n",
    "\n",
    "}  \n",
    "\n",
    "\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- A **`for`** loop in C has three control components in addition to the loop body:\n",
    "    - initialization of the loop control variable,\n",
    "    - testing of the loop repetition condition, and \n",
    "    - changing (updating) of the loop control variable. \n",
    "- The **`for`** statement in C supplies a designated place for each of these three components."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_style": "center",
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "### Program Style - Formatting the for Statement \n",
    "- For clarity, we usually place each expression of the **`for`** heading on a separate line.\n",
    "- If all three expressions are very short, we may place them together on one line.\n",
    "- For example, "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_style": "center",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "source": [
    "```C  \n",
    "\n",
    "/* Compute the payroll for a company */\n",
    "\n",
    "#include <stdio.h>\n",
    "\n",
    "int\n",
    "\n",
    "main(void)\n",
    "\n",
    "{\n",
    "\n",
    "    double total_pay; /* company payroll */\n",
    "    \n",
    "    int count_emp; /* current employee */\n",
    "    \n",
    "    int number_emp; /* number of employees */\n",
    "    \n",
    "    double hours; /* hours worked */\n",
    "    \n",
    "    double rate; /* hourly rate */\n",
    "    \n",
    "    double pay; /* pay for this period */\n",
    "\n",
    "    \n",
    "    /* Get number of employees. */\n",
    "    \n",
    "    printf(\"Enter number of employees> \");\n",
    "    \n",
    "    scanf(\"%d\", &number_emp);\n",
    "\n",
    "    \n",
    "    /* Compute each employee's pay and add it to the payroll. */\n",
    "    \n",
    "    total_pay = 0.0;\n",
    "    \n",
    "    for (count_emp = 0;  /* initialization */\n",
    "    \n",
    "         count_emp < number_emp;  /* loop repetition condition */\n",
    "         \n",
    "         count_emp += 1) {  /* update */        \n",
    "    \n",
    "        printf(\"Hours> \");\n",
    "        \n",
    "        scanf(\"%lf\", &hours);\n",
    "        \n",
    "        printf(\"Rate > $\");\n",
    "        \n",
    "        scanf(\"%lf\", &rate);\n",
    "        \n",
    "        pay = hours * rate;\n",
    "        \n",
    "        printf(\"Pay is $%6.2f\\n\\n\", pay);\n",
    "        \n",
    "        total_pay = total_pay + pay; /* Add next pay. */\n",
    "\n",
    "    }\n",
    "\n",
    "    printf(\"All employees processed\\n\");\n",
    "    \n",
    "    printf(\"Total payroll is $%8.2f\\n\", total_pay);\n",
    "\n",
    "return (0);\n",
    "  \n",
    "}  \n",
    "  \n",
    "  \n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "### Increment and Decrement Operators\n",
    "- The for loops examples we have seen all included assignment expression of the form\n",
    "    \n",
    "```C\n",
    "    \n",
    "    counter = counter + 1;\n",
    "\n",
    "    // OR\n",
    "\n",
    "    counter += 1;\n",
    "\n",
    "    // OR\n",
    "\n",
    "    counter ++;\n",
    "    \n",
    "\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- The increment operator **++** takes a single variable as its operand. \n",
    "- The **side effect** of applying the **++** operator is that the value of its operand is incremented by one.\n",
    "- Frequently, **++** is used just for this side effect, as in the following loop in which the variable **`counter`** is to run from **0** up to **`limit`**:\n",
    "\n",
    "```C\n",
    "    \n",
    "    for (counter = 0; counter < limit; counter++) {\n",
    "        \n",
    "        ...\n",
    "            \n",
    "    }\n",
    "    \n",
    "    \n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "- The value of the expression in which the **++** operator is used depends on the **position** of the operator. \n",
    "- When the **++** is placed immediately **in front of** its operand (prefix increment), the value of the expression is the variable’s value **after** incrementing.\n",
    "- When the **++** comes immediately **after** the operand (postfix increment), the expression’s value is the value of the variable **before** it is incremented. \n",
    "- Compare the action of the two code segments in following figure, given an initial value of **2** in **`i`**."
   ]
  },
  {
   "attachments": {
    "C-book-ch02fig19.PNG": {
     "image/png": "[encoded data, 25264 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "![C-book-ch02fig19.PNG](attachment:C-book-ch02fig19.PNG)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "- C also provides a decrement operator that can be used in either the prefix or postfix position. \n",
    "- For example, if the initial value of **`n`** is **4**"
   ]
  },
  {
   "attachments": {
    "s1.PNG": {
     "image/png": "[encoded data, 4476 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {
    "cell_style": "center",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "source": [
    "![s1.PNG](attachment:s1.PNG) print"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_style": "center",
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "`\n",
    "  3  3\n",
    "`\n",
    "\n",
    "- Again, if the initial value of **`n`** is **4**, the following lines of code"
   ]
  },
  {
   "attachments": {
    "s2.PNG": {
     "image/png": "[encoded data, 5308 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {
    "cell_style": "center",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "source": [
    "![s2.PNG](attachment:s2.PNG) print"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_style": "center",
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "`\n",
    "  4  3\n",
    "`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "### Example\n",
    "- Function factorial computes the factorial of an integer represented by the formal parameter **`n`**. \n",
    "- The loop body executes for decreasing values of **`i`** from **`n`** down to **2**, and each value of **`i`** is incorporated in the accumulating product. \n",
    "- The loop exit occurs when **`i`** is **1**."
   ]
  },
  {
   "attachments": {
    "C-book-ch02fig21.PNG": {
     "image/png": "[encoded data, 101316 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "![C-book-ch02fig21.PNG](attachment:C-book-ch02fig21.PNG)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "```C  \n",
    "\n",
    "#include <stdio.h>\n",
    "\n",
    "\n",
    "\n",
    "int factorial (int n) ; /* function prototype (declaration) */\n",
    "\n",
    "\n",
    "\n",
    "int\n",
    "\n",
    "main(void)\n",
    "\n",
    "{\n",
    "\n",
    "    int x, fact_x;\n",
    "    \n",
    "    printf(\"Enter the value of x: \");\n",
    "    \n",
    "    scanf(\"%d\", &x);\n",
    "    \n",
    "    fact_x = factorial(x);\n",
    "    \n",
    "    printf(\"The factorial of %d = %d\\n\", x, fact_x);\n",
    "    \n",
    "    return (0);\n",
    "\n",
    "}\n",
    "\n",
    "\n",
    "/*\n",
    "\n",
    " * Computes n!\n",
    "\n",
    " * Pre: n is greater than or equal to zero\n",
    "\n",
    " */\n",
    "\n",
    "int\n",
    "\n",
    "factorial(int n)\n",
    "\n",
    "{\n",
    "\n",
    "    int i, /* local variables */\n",
    "    \n",
    "        product; /* accumulator for product computation */\n",
    "    \n",
    "    \n",
    "    \n",
    "    product = 1;\n",
    "    \n",
    "    \n",
    "    \n",
    "    /* Computes the product n x (n-1) x (n-2) x . . . x 2 x 1 */\n",
    "    \n",
    "    for (i = n; i > 1; --i) {\n",
    "    \n",
    "        product = product * i;\n",
    "    \n",
    "    }\n",
    "    \n",
    "    /* Returns function result */\n",
    "    \n",
    "    return (product);\n",
    "\n",
    "}\n",
    "\n",
    "\n",
    "```"
   ]
  },
  {
   "attachments": {
    "C-book-ch02fig22.PNG": {
     "image/png": "[encoded data, 30024 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {
    "cell_style": "center",
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "### Nested Loops\n",
    "- Consist of an outer loop with one or more inner loops.\n",
    "- Each time the outer loop is repeated, the inner loop is reentered and executed. \n",
    "- Example\n",
    "![C-book-ch02fig22.PNG](attachment:C-book-ch02fig22.PNG)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_style": "center",
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "```C\n",
    "\n",
    "\n",
    "#include <stdio.h>\n",
    "\n",
    "\n",
    "\n",
    "void stars (int n); /* Function prototype (declaration) */\n",
    "\n",
    "\n",
    "\n",
    "int\n",
    "\n",
    "main(void)\n",
    "\n",
    "{\n",
    "\n",
    "    int x;\n",
    "    \n",
    "    printf(\"Enter the maximum number of stars: \");\n",
    "    \n",
    "    scanf(\"%d\", &x);\n",
    "    \n",
    "    printf(\"\\nThe result of stars(%d) is:\\n\", x);\n",
    "    \n",
    "    stars(x);\n",
    "    \n",
    "    return (0);\n",
    "\n",
    "}\n",
    "\n",
    "\n",
    "\n",
    "void stars (int n) {\n",
    "\n",
    "    int i, j;\n",
    "    \n",
    "    for (i=1; i<=n; i++) {\n",
    "    \n",
    "        for (j=1; j<=i; j++) {\n",
    "        \n",
    "            printf(\"*\");\n",
    "        \n",
    "        }\n",
    "        \n",
    "        printf(\"\\n\");\n",
    "    \n",
    "    }\n",
    "\n",
    "}\n",
    "\n",
    "\n",
    "```"
   ]
  },
  {
   "attachments": {
    "C-book-ch02fig26.PNG": {
     "image/png": "[encoded data, 51412 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# The do-while Statement\n",
    "- The for and while statements evaluate the loop condition before the execution of the loop body.\n",
    "- The do-while statement evaluates the loop condition after the execution of the loop body\n",
    "- Syntax:\n",
    "![C-book-ch02fig26.PNG](attachment:C-book-ch02fig26.PNG)"
   ]
  },
  {
   "attachments": {
    "C-book-ch02fig26-1.PNG": {
     "image/png": "[encoded data, 64560 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "![C-book-ch02fig26-1.PNG](attachment:C-book-ch02fig26-1.PNG)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Using do-while to Repeat Program"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "source": [
    "```C\n",
    "\n",
    "#include <stdio.h>\n",
    "\n",
    "\n",
    "\n",
    "int\n",
    "\n",
    "main(void)\n",
    "\n",
    "{\n",
    "\n",
    "    char ch;\n",
    "    \n",
    "    ch = 'y';            /* User response [y/n] */ \n",
    "    \n",
    "    do {\n",
    "    \n",
    "        printf(\"%c\\n\", ch); /* display character */\n",
    "        \n",
    "        printf(\"Repeat again [y/n]? \");\n",
    "        \n",
    "        scanf(\" %c\",&ch); /* read character */ \n",
    "    \n",
    "    } while (ch=='y' || ch == 'Y');\n",
    "    \n",
    "    return (0);\n",
    "\n",
    "}\n",
    "\n",
    "\n",
    "```"
   ]
  }
 ],
 "metadata": {
  "celltoolbar": "Slideshow",
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  },
  "rise": {
   "theme": "yellow"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
